package com.szly.phm.admin.controller;

import com.szly.phm.admin.aspectj.annotation.OperationLogAnnotation;
import com.szly.phm.admin.aspectj.enums.ModuleEnum;
import com.szly.phm.admin.aspectj.enums.OperationActionEnum;
import com.szly.phm.admin.utils.CustomResponse;
import com.szly.phm.admin.utils.SecurityUtils;
import com.szly.phm.common.entity.dto.operation.CreateOperationDTO;
import com.szly.phm.common.entity.dto.operation.LogPageDTO;
import com.szly.phm.common.entity.dto.operation.OperationPageDTO;
import com.szly.phm.common.entity.dto.operation.UpdateOperationDTO;
import com.szly.phm.common.entity.dto.user.LoginUserDTO;
import com.szly.phm.common.entity.result.RootResponse;
import com.szly.phm.common.service.ISysOperationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;

@RestController
@Validated
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@RequestMapping("/pc/operation")
@Api(tags = "功能管理")
//@PreAuthorize("hasAuthority('main-system-menu') or hasAuthority('supperAdmin')")
public class SysOperationController {
    private final ISysOperationService sysOperationService;


    @ApiOperation(value = "添加功能管理")
    @OperationLogAnnotation(getModuleName = ModuleEnum.PERMISSION,getOperationAction = OperationActionEnum.SAVE)
    @PostMapping("/v1/operation")
    @CustomResponse
    public RootResponse createOperationV1(@Valid @RequestBody CreateOperationDTO createOperationDTO) {
        var loginUser = SecurityUtils.getLoginUser();
        var userDTO = new LoginUserDTO();
        userDTO.setUsername(loginUser.getUsername());
        return sysOperationService.createOperation(createOperationDTO,userDTO);
    }
    @ApiOperation(value = "修改功能管理")
    @CustomResponse
    @OperationLogAnnotation(getModuleName = ModuleEnum.PERMISSION,getOperationAction = OperationActionEnum.UPDATE)
    @PutMapping("/v1/operation")
    public RootResponse updateOperationV1(@Valid @RequestBody UpdateOperationDTO updateOperationDTO) {
        var loginUser = SecurityUtils.getLoginUser();
        var userDTO = new LoginUserDTO();
        userDTO.setUsername(loginUser.getUsername());
        return sysOperationService.updateOperation(updateOperationDTO,userDTO);
    }
    @ApiOperation(value = "删除职业名称类型")
    @OperationLogAnnotation(getModuleName = ModuleEnum.PERMISSION,getOperationAction = OperationActionEnum.DELETE)
    @DeleteMapping("/v1/operation")
    @CustomResponse
    public RootResponse deleteJobNameV1(@NotBlank(message = "用户类型Id不能为空") @RequestParam(value = "id") String id) {
        var loginUser = SecurityUtils.getLoginUser();
        var userDTO = new LoginUserDTO();
        userDTO.setUsername(loginUser.getUsername());
        return sysOperationService.deleteOperation(id,userDTO);
    }

    @ApiOperation(value = "分页查询功能列表")
    @GetMapping("/v1/operation")
    @CustomResponse
    public RootResponse queryOperationPageV1(@Valid OperationPageDTO pageDTO) {
        return sysOperationService.queryOperationPageV1(pageDTO);
    }
    @ApiOperation(value = "分页日志列表")
    @GetMapping("/v1/logs")
    @CustomResponse
    public RootResponse queryLoginPageV1(@Valid LogPageDTO pageDTO) {
        return sysOperationService.queryLoginPageV1(pageDTO);
    }
}
